package smap;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BD.TurmaBD;
import DAO.TurmaDAO;

public class TelaTurma extends javax.swing.JDialog {

    TurmaBD tur = new TurmaBD();
    TurmaDAO turDao = new TurmaDAO();
    
    public TelaTurma(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnProfessor = new javax.swing.JButton();
        btnUsuario = new javax.swing.JButton();
        btnCurso = new javax.swing.JButton();
        btnDisciplina = new javax.swing.JButton();
        btnAluno = new javax.swing.JButton();
        btnBackup = new javax.swing.JButton();
        btnTurma = new javax.swing.JButton();
        pnlControle = new javax.swing.JPanel();
        txtPesquisaTurma = new javax.swing.JTextField();
        cmbPesquisar = new javax.swing.JComboBox();
        btnPesquisar = new javax.swing.JButton();
        btnCarregartodos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTurma = new javax.swing.JTable();
        btnCadastrar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        btnVoltar3 = new javax.swing.JButton();
        btnAdministrarTurma = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela Turma");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/professor-icon.png"))); // NOI18N
        btnProfessor.setText("Professor");
        btnProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfessorActionPerformed(evt);
            }
        });

        btnUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/user-icon.png"))); // NOI18N
        btnUsuario.setText("Usuario");
        btnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUsuarioActionPerformed(evt);
            }
        });

        btnCurso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/curso-icon.png"))); // NOI18N
        btnCurso.setText("Curso");
        btnCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCursoActionPerformed(evt);
            }
        });

        btnDisciplina.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/disciplina-icon.png"))); // NOI18N
        btnDisciplina.setText("Disciplina");
        btnDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisciplinaActionPerformed(evt);
            }
        });

        btnAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/aluno-icon.png"))); // NOI18N
        btnAluno.setText("Aluno");
        btnAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlunoActionPerformed(evt);
            }
        });

        btnBackup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/backup-icon.png"))); // NOI18N
        btnBackup.setText("Backup");
        btnBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupActionPerformed(evt);
            }
        });

        btnTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/turma-icon.png"))); // NOI18N
        btnTurma.setText("Turma");
        btnTurma.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProfessor)
                    .addComponent(btnUsuario)
                    .addComponent(btnAluno)
                    .addComponent(btnBackup)
                    .addComponent(btnDisciplina)
                    .addComponent(btnCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnTurma))
                .addContainerGap())
        );

        pnlControle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)), "Pesquisar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.darkGray));

        cmbPesquisar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "Curso", "Ano" }));

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/procurar-icon.png"))); // NOI18N
        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnCarregartodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/todos-icon.png"))); // NOI18N
        btnCarregartodos.setText("Carregar todos");
        btnCarregartodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregartodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlControleLayout = new javax.swing.GroupLayout(pnlControle);
        pnlControle.setLayout(pnlControleLayout);
        pnlControleLayout.setHorizontalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtPesquisaTurma)
                .addGap(18, 18, 18)
                .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCarregartodos, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlControleLayout.setVerticalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPesquisaTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar)
                    .addComponent(btnCarregartodos))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblTurma.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));
        tblTurma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nome", "Curso", "Ano"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblTurma.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblTurma.getTableHeader().setResizingAllowed(false);
        tblTurma.getTableHeader().setReorderingAllowed(false);
        tblTurma.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTurmaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblTurma);
        tblTurma.getColumnModel().getColumn(0).setMinWidth(80);
        tblTurma.getColumnModel().getColumn(0).setPreferredWidth(80);
        tblTurma.getColumnModel().getColumn(0).setMaxWidth(100);
        tblTurma.getColumnModel().getColumn(3).setMinWidth(120);
        tblTurma.getColumnModel().getColumn(3).setPreferredWidth(100);
        tblTurma.getColumnModel().getColumn(3).setMaxWidth(120);

        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/delete-icon.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alterar-icon.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar3.setText("Voltar");
        btnVoltar3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltar3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(524, 524, 524)
                .addComponent(btnVoltar3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAdministrarTurma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/turma-icon.png"))); // NOI18N
        btnAdministrarTurma.setText("Administrar turmas");
        btnAdministrarTurma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdministrarTurmaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
            .addComponent(pnlControle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAdministrarTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(218, 218, 218)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnCadastrar)
                    .addComponent(btnAlterar)
                    .addComponent(btnAdministrarTurma))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfessorActionPerformed
        this.dispose(); //Fecha a Tela de Turma
        TelaProfessor pro = new TelaProfessor(null, true);
        pro.carregarTabela("");
        pro.setVisible(true); // Abre Tela Professor
    }//GEN-LAST:event_btnProfessorActionPerformed

    private void btnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUsuarioActionPerformed
        this.dispose();  //Fecha a Tela de Turma
        TelaUsuario usu = new TelaUsuario(null, true);
        usu.carregarTabela("");
        usu.setVisible(true); //Abre Tela Usuario
    }//GEN-LAST:event_btnUsuarioActionPerformed

    private void btnCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCursoActionPerformed
        this.dispose(); //Fecha a Tela de Turma
        TelaCurso cur = new TelaCurso(null, true);
        cur.carregarTabela("");
        cur.setVisible(true); // Abre Tela Curso
    }//GEN-LAST:event_btnCursoActionPerformed

    private void btnAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlunoActionPerformed
        this.dispose(); //Fecha a Tela de Turma
        TelaAluno alu = new TelaAluno(null, true);
        alu.carregarTabela("");
        alu.setVisible(true); // Abre Tela Aluno
    }//GEN-LAST:event_btnAlunoActionPerformed

    private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
        this.dispose(); //Fecha a Tela de Turma
        TelaBackup bac = new TelaBackup(null, true);
        bac.setVisible(true); // Abre Tela Backup
    }//GEN-LAST:event_btnBackupActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        //lista as turmas de acordo com o que foi digitado
        carregarTabela(txtPesquisaTurma.getText().trim());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnCarregartodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregartodosActionPerformed
        //lista todas as turmas
        carregarTabela("");
    }//GEN-LAST:event_btnCarregartodosActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        this.enable();
        TelaCadTurma cadtur = new TelaCadTurma(null, true);
        cadtur.setCadastrar(true);//true para cadastrar
        cadtur.carregarComboCurso();
        cadtur.autoIncremento();
        cadtur.setVisible(true);
        //Recarregar a tabela
        carregarTabela("");
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if(tblTurma.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione uma turma para excluir",
                    "Selecione a turma", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Object[] opcoes = { "Sim", "Não" };
             //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblTurma.getSelectedRow();
            Integer resposta =JOptionPane.showOptionDialog(null,
                    "Deseja excluir esta turma ?", "Exclusão",JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);
            
            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){
                
                // Seta o cod, o nome da turma, nome do curso e o ano.
                tur.setCodigo((Integer)tblTurma.getValueAt(linha, 0));
                tur.setNome((String)tblTurma.getValueAt(linha, 1));
                tur.setNomeCursoAux((String)tblTurma.getValueAt(linha, 2));
                tur.setAno((Integer)tblTurma.getValueAt(linha, 3));

                //excluir              
                if(turDao.excluirTurma(tur)){
                    JOptionPane.showMessageDialog(null,"Exclusão realizada com sucesso", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);   
                    //recarregar o jTable
                    carregarTabela("");
                }
                else{
                    JOptionPane.showMessageDialog(null,"Nao foi possível realizar a exclusão", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Exclusão cancelada", 
                        "Exclusão", JOptionPane.INFORMATION_MESSAGE);
            }
        }             
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (tblTurma.getSelectedRow() == -1) { // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null, "Selecione uma turma para alterar",
                    "Selecione a turma", JOptionPane.INFORMATION_MESSAGE);
        } else {
            //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblTurma.getSelectedRow();
            tur.setCodigo((Integer) tblTurma.getValueAt(linha, 0));
            tur.setNome((String) tblTurma.getValueAt(linha, 1));
            tur.setNomeCursoAux((String) tblTurma.getValueAt(linha, 2));
            tur.setAno((Integer) tblTurma.getValueAt(linha, 3));
            
            //Manda os dados para a tela de Alterar
            this.enable();
            TelaCadTurma alttur = new TelaCadTurma(null, true);
            alttur.carregarComboCurso(); //carregar os cursos
            alttur.recebeObjejo(tur);
            alttur.setCadastrar(false);//false para alterar
            alttur.setTitle("Alterar Turma");
            alttur.setVisible(true);
           
            //Recarregar a tabela
            carregarTabela(txtPesquisaTurma.getText().trim());
        } 
   }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnVoltar3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltar3ActionPerformed
        this.dispose(); // Fecha Tela Curso
    }//GEN-LAST:event_btnVoltar3ActionPerformed

    private void tblTurmaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTurmaMouseClicked
        if(evt.getClickCount() == 2){
            if(tblTurma.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
                JOptionPane.showMessageDialog(null,"Selecione um curso para visualizar", "Selecione o curso", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {    
                Integer linha = tblTurma.getSelectedRow();
                //seta o objeto curso de acordo com o que foi selecionado
                tur.setCodigo((Integer)tblTurma.getValueAt(linha, 0));
                tur.setNome((String)tblTurma.getValueAt(linha, 1));
                tur.setNomeCursoAux((String)tblTurma.getValueAt(linha, 2)); 
                tur.setAno((Integer)tblTurma.getValueAt(linha, 3));
                //Manda os dados para a tela de Alterar
                this.enable();
                TelaCadTurma visutur = new TelaCadTurma(null, true);
                visutur.carregarComboCurso();
                visutur.visualizarObjeto(tur);
                visutur.setTitle("Visualizar Turma");
                visutur.setVisible(true);
            }
       }
    }//GEN-LAST:event_tblTurmaMouseClicked

    private void btnAdministrarTurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdministrarTurmaActionPerformed
        this.enable();
        TelaTurmaDisciplina turdis = new TelaTurmaDisciplina(null, true);
        turdis.carregarComboTurma(Calendar.getInstance().get(Calendar.YEAR)); //carregar as turmas
        turdis.carregarComboProfessor(); // carregar professor
        turdis.carregarComboCurso(); // carregar cursos
        turdis.carregarComboArea(); //carregar areas
        turdis.setVisible(true);
    }//GEN-LAST:event_btnAdministrarTurmaActionPerformed

    private void btnDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisciplinaActionPerformed
        this.dispose(); //Fecha a Tela de Turma
        TelaDisciplina dis = new TelaDisciplina(null, true);
        dis.carregarTabela("");
        dis.setVisible(true); // Abre Tela Disciplina
    }//GEN-LAST:event_btnDisciplinaActionPerformed

    public void carregarTabela(String valor){
        List<TurmaBD> listaTurma = new ArrayList<TurmaBD>();
               
        if(cmbPesquisar.getSelectedIndex() == 0){          
            listaTurma = turDao.filtro("t.nome_turma", valor);
        }else{
            if(cmbPesquisar.getSelectedIndex() == 1){
                listaTurma = turDao.filtro("c.nome_curso", valor);
            }else
            {
                listaTurma = turDao.filtro("t.ano_turma", valor);
            }
        }
         
        DefaultTableModel tbm = (DefaultTableModel)tblTurma.getModel();    

        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        int i = 0; 
        for(TurmaBD tr : listaTurma){            
        tbm.addRow(new String[1]); 
            tblTurma.setValueAt(tr.getCodigo(), i, 0); //Objeto Linha Coluna
            tblTurma.setValueAt(tr.getNome(), i, 1); 
            tblTurma.setValueAt(tr.getNomeCursoAux(), i, 2);
            tblTurma.setValueAt(tr.getAno(), i, 3); 
            i++;
        }
         centralizarTable();
    }

    public void centralizarTable(){
        DefaultTableCellRenderer centro = new DefaultTableCellRenderer();
        centro.setHorizontalAlignment(SwingConstants.CENTER ); 
        tblTurma.getColumnModel().getColumn(0).setCellRenderer(centro);
        tblTurma.getColumnModel().getColumn(3).setCellRenderer(centro);     
    }
    
    
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaTurma dialog = new TelaTurma(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdministrarTurma;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAluno;
    private javax.swing.JButton btnBackup;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCarregartodos;
    private javax.swing.JButton btnCurso;
    private javax.swing.JButton btnDisciplina;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnProfessor;
    private javax.swing.JButton btnTurma;
    private javax.swing.JButton btnUsuario;
    private javax.swing.JButton btnVoltar3;
    private javax.swing.JComboBox cmbPesquisar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlControle;
    private javax.swing.JTable tblTurma;
    private javax.swing.JTextField txtPesquisaTurma;
    // End of variables declaration//GEN-END:variables
}
